package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.Date;
import java.util.Calendar;
import java.util.List;

@Service
@Transactional
public class OperationLogService {

	@Autowired
	private OperationLogDao operationLogDao;

	private QOperationLog $ = QOperationLog.operationLog;

	@Autowired
	public void setOperationLogDao(OperationLogDao operationLogDao) {
		this.operationLogDao = operationLogDao;
	}

	public List<OperationLog> findAll() {
		return ListUtil.toList(operationLogDao.findAll());
	}

	public List<OperationLog> findAll(String operationType, String object,
			String operationDescription, Long personId, Long proxyPersonId,
			String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (StringUtil.isNotEmpty(operationType)) {
			condition.and($.operationType.contains(operationType));
		}

		if (StringUtil.isNotEmpty(object)) {
			condition.and($.objectID.contains(object));
		}

		if (StringUtil.isNotEmpty(operationDescription)) {
			condition
					.and($.operationDescription.contains(operationDescription));
		}

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}

		if (IdUtil.isNotEmpty(proxyPersonId)) {
			condition.and($.proxyPerson.id.eq(proxyPersonId));
		}

		return ListUtil.toList(operationLogDao.findAll(condition));
	}

	public Page<OperationLog> findAll(Date startDatetime, Date endDatetime,
			String operationType, String object, String operationDescription,
			Long personId, Long proxyPersonId, int pageNumber, int pageSize,
			String sortAttribute, String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();
		if (null != startDatetime) {
			condition.and($.operationDateTime.loe(startDatetime));
		}
		if (null != endDatetime) {
			condition.and($.operationDateTime.goe(endDatetime));
		}
		if (StringUtil.isNotEmpty(operationType)) {
			condition.and($.operationType.contains(operationType));
		}
		if (StringUtil.isNotEmpty(object)) {
			condition.and($.objectID.contains(object));
		}
		if (StringUtil.isNotEmpty(operationDescription)) {
			condition
					.and($.operationDescription.contains(operationDescription));
		}
		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}
		if (IdUtil.isNotEmpty(proxyPersonId)) {
			condition.and($.proxyPerson.id.eq(proxyPersonId));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return operationLogDao.findAll(condition, pageRequest);
	}

	public OperationLog getOperationLog(Long id) {
		return operationLogDao.getOne(id);
	}

	public void deleteOperationLog(Long id) {
		operationLogDao.delete(id);
	}

	public void saveOperationLog(OperationLog operationLog) {
		if (null == operationLog.getOperationDateTime()) {
			//DateUtil.nowDate()
			operationLog.setOperationDateTime(Calendar.getInstance().getTime());
		}
		operationLogDao.save(operationLog);
	}

	public void log(Long personId, Long proxyPersonId, String operationType, String object, String operationDescription){
		OperationLog operationLog = new OperationLog();
		People person = new People();
		person.setId(personId);
		operationLog.setPerson(person);
		People proxy = new People();
		proxy.setId(proxyPersonId);
		operationLog.setProxyPerson(proxy);
		operationLog.setObjectID(object);
		operationLog.setOperationType(operationType);
		operationLog.setOperationDescription(operationDescription);
		
		this.saveOperationLog(operationLog);
	}

	public OperationLog findOne(Long id) {
		return this.operationLogDao.findOne(id);
	}
	
}
